package com.yumijinfu.presenter;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.widget.Toast;

import com.lzy.okhttputils.model.HttpParams;
import com.yumijinfu.http.HttpCallBack;
import com.yumijinfu.http.HttpUtils;
import com.yumijinfu.iview.ILoginView;
import com.yumijinfu.mian.Config;
import com.yumijinfu.tool.SharedPreferencesUtils;
import com.yumijinfu.tool.VibratorUtil;
import com.yumijinfu.util.DES;
import com.yumijinfu.view.ClearEditText;

import org.json.JSONObject;

/**
 * Created by ekikousei易皇星 on 16/11/27.
 * E-mail:13764664731@163.com
 * Signature:缘分是本书,翻的不经意会错过,读的太认真会流泪!!
 * <p/>
 * TODO:类描述:
 */
public class LoginPresenterCompl implements LoginPresenter {
    private ILoginView mloginview;
    private Context context;
    private Activity activity;

    public LoginPresenterCompl(ILoginView mloginview, Context context,Activity activity) {
        this.mloginview = mloginview;
        this.context = context;
        this.activity=activity;
    }

    //做业务逻辑处理
    long[] pattern = {100, 400, 100, 400};
    @Override
    public void doLogin(final  String phone, String password, String code, String codeStr, ClearEditText etPhone, ClearEditText etPassword, ClearEditText etCode, int flag) {
        if (TextUtils.isEmpty(phone)) {
            etPhone.setShakeAnimation();
            VibratorUtil.Vibrate(context, pattern, false);
            Toast.makeText(activity, "请输入手机号", Toast.LENGTH_SHORT).show();
            return;

        } else if (TextUtils.isEmpty(password)) {
            etPassword.setShakeAnimation();
            VibratorUtil.Vibrate(context, pattern, false);
            Toast.makeText(activity, "请输入密码", Toast.LENGTH_SHORT).show();
            return;

        } else if (TextUtils.isEmpty(code)) {
            etCode.setShakeAnimation();
            VibratorUtil.Vibrate(context, pattern, false);
            Toast.makeText(activity, "请输入验证码", Toast.LENGTH_SHORT).show();
            return;

        } else if (!codeStr.equals(code)) {
            VibratorUtil.Vibrate(context, pattern, false);
            etCode.setShakeAnimation();
            Toast.makeText(activity, "验证码输入错误", Toast.LENGTH_SHORT).show();
            return;

        } else {
            try {
                HttpParams params=new HttpParams();
                params.put("userAccount",phone);
                params.put("userPassword", DES.encryptDES(password,Config.DES_KEY));
                HttpUtils.post(Config.LOGIN, params, new HttpCallBack(activity,"登录中...") {
                    @Override
                    public void onSucceed(JSONObject result, String code, String message) {
                        // 登录成功
                        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
                        SharedPreferencesUtils.getInstance().put(context,"userAccount",phone);
                        if(null!=result){
                            mloginview.Result(result);
                        }
                    }

                    @Override
                    public void onError(String code, String message) {
                        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
                    }
                });

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
